<?php
namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Helpers\WebResponse;
use App\Services\WxCommon\EasyWechat;

/**
 * 基类控制器
 * @package App\Http\Controllers\Wechat
 */
class BaseController extends Controller
{
    use WebResponse;
    /**
     * 视图数据
     * @var array
     */
    protected $viewData = [];
    protected $wxConfigApis = [];

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->initWxConfig(['onMenuShareTimeline','onMenuShareAppMessage']);
    }

    /**
     * 初始化微信js-sdk
     * @param array $apis
     * @param bool $debug
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function initWxConfig($apis = [], $debug = false)
    {
        //叠加JS接口列表
        if (empty($this->wxConfigApis)){
            $this->wxConfigApis = $apis;
        }else{
            $this->wxConfigApis = array_merge($this->wxConfigApis, $apis);
        }
        //微信js-sdk
        $app = EasyWechat::officialAccount();
        $app->jssdk->setUrl(request()->root() . '/new' . $_SERVER['REQUEST_URI']);  //
//        $app->jssdk->setUrl(request()->root(). '/new/' .request()->path());
        $this->viewData['wxConfig'] = $app->jssdk->buildConfig($this->wxConfigApis, $debug);
    }
}
